package com.example.service.product.impl;

import com.example.dao.product.PriductCategroyDao;
import com.example.pojo.Node;
import com.example.pojo.PriductCategroy;
import com.example.service.product.PriductCategroyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class PriductCategroyServiceImpl implements PriductCategroyService {
    @Autowired
    PriductCategroyDao dao;

    @Override
    public List<Node> getPriductCategroy() {
        List<PriductCategroy> all = dao.getPriductCategroy();
        List<Node> childs = getNode(all, 0);
        return childs;
    }

    private List<Node> getNode( List<PriductCategroy> all,int parentId) {
        List<Node> list = new ArrayList<>();
        for (PriductCategroy productList : all) {
            if (productList.getParentId() == parentId){
                Node node = new Node();
                node.setTitle(productList.getTitle());
                node.setId(productList.getCategrayId());
                List<Node> childs = getNode(all, productList.getCategrayId());
                node.setChildren(childs);
                list.add(node);
            }
        }

        return list;
    }

}
